 package com.ezhuan.outer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.controller.BaseController;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.system.pojo.ASmsHistory;
import com.ezhuan.system.pojo.ASmsHistoryEnum;
import com.ezhuan.system.service.ISystemService;
import com.ezhuan.system.service.impl.SystemServiceImplay;
import com.ezhuan.users.pojo.AUsers;
import com.ezhuan.users.pojo.AUsersEnum;
import com.ezhuan.users.service.IUsersService;
import com.ezhuan.users.service.impl.UsersServiceImpl;

/**
 * 公共的Controller
 * @author lenovo
 *
 */
public class CommonOuterController extends BaseController
{
	private ISystemService systemService = new SystemServiceImplay() ; 
	private IUsersService usersService = new UsersServiceImpl();
	
	/**
	 * 发送短信验证码
	 */
	public void sendSmsCode()
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* 电话 */
		String phone = getPara("phone");
		if(phone == null)
		{
			phone = "" ; 
		}
		
		try
		{
			if(!"".equalsIgnoreCase(phone))
			{
				/* 参数都不为空 */
				/* 发送短信 */
				/* 生成的随机数 */
				String checknum = this.regexUtil.randStr(6);
				/* 模板号 :正式环境为30*/
				String yxtime = "30" ; 
				
				ASmsHistory smsHistory = new ASmsHistory();
				smsHistory.setSn(this.dateFormatUtil.format(new Date(),"yyyyMMddHHmmssS"));
				smsHistory.setPhone(phone);
				smsHistory.setTemplateSn(yxtime);
				smsHistory.setCode(checknum);
				smsHistory.setContent("");
				smsHistory.setCreateTime(new Date());
				smsHistory.setUpdateTime(new Date());
				smsHistory.setPubTime(new Date());
				/* 保存数据 */
				resultJSON = this.systemService.saveOneSmsHistoryService(smsHistory);
			}else
			{
				/* 上行参数非法 */
				resultJSON.put("code", "5");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("5"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("发送短信;上行数据:{}",this.getParaMap(), e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		
		/* 返回结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 查看用户的详细信息
	 */
	public void usersLogin()
	{
		ConstatFinalUtil.LOGGER.info("======usersInfo======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		/* 接收参数 */
		String phone = this.getPara("phone");
		String password = this.getPara("password");
		if (phone == null)
		{
			phone = "";
		}
		if (password == null)
		{
			password = "";
		}
		try
		{
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("phone", phone);
			condMap.put("extend", "true");
			AUsers users = this.usersService.findOneUsersService(condMap);
			/* 查询结果 */
			if (users != null)
			{
				if(encryptUtil.checkPass(password, users.getPassword()))
				{
					/* 密码正确 */
					if(users.getStatus() == 1)
					{
						/* 登陆成功 */
						JSONObject dataJSON = users.toJSON();
						resultJSON.put("data", dataJSON);
						resultJSON.put("code", "0");
						resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
						
					}else
					{
						/* 账号被禁用 */
						resultJSON.put("code", "11");
						resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("11"));
					}
				}else
				{
					/* 密码不正确 */
					resultJSON.put("code", "10");
					resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("10"));
				}
			} else
			{
				/* 手机号不存在 */
				resultJSON.put("code", "9");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("9"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("查询用户详细报错了:phone:{}", phone, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 用户注册
	 * 需要手机号,密码
	 * @return
	 */
	public void register()
	{
		ConstatFinalUtil.LOGGER.info("======usersInsertSubmit======");
		long st = System.currentTimeMillis() ;
		JSONObject resultJSON = new JSONObject();
		
		/* 接收参数 */
		String phone = this.getPara("phone");
		String password = this.getPara("password");
		/* 用户输入的短信验证码 */
		String code = this.getPara("code");
		/* 短信验证码的Id */
		String messId = this.getPara("messId");
		/* 邀请码 */
		String invitationCode = this.getPara("invitationCode");
		
		try
		{
			Map<String,Object> condMap = new HashMap<String, Object>();
			/* 短信验证 */
			if(messId == null)
			{
				messId = "" ; 
			}
			if(!"".equalsIgnoreCase(messId))
			{
				condMap.put("id", messId);
				ASmsHistory smsHistory = this.systemService.findOneSmsHistoryService(condMap);
				/* 验证短信的验证码 */
				if(smsHistory != null && smsHistory.getCode().equalsIgnoreCase(code)
					&& smsHistory.getStatus() == 1)
				{
					/* 短信验证通过 */
					smsHistory.setStatus(ASmsHistoryEnum.STATUS_VERIFY.getStatus());
					smsHistory.setUpdateTime(new Date());
					this.systemService.updateOneSmsHistoryService(smsHistory);
				}else
				{
					/* 短信验证码不正确 */
					resultJSON.put("code", "7");
					resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("7"));
					renderJson(resultJSON);
					return ; 
				}
			}else
			{
				/* 短信验证码不正确 */
				resultJSON.put("code", "7");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("7"));
				renderJson(resultJSON);
				return ; 
			}
			
			/* 创建对象,赋值 */
			AUsers users = new AUsers();
			users.setPhone(phone);
			/* 密码加密,采用SHA256 */
			users.setPassword(this.encryptUtil.encodePass(password));
			/* 设置为启用 */
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStauts());
			/* 设置注册IP */
			users.setRegIp(this.getRequest().getRemoteAddr());
			/* 用户填写别人的邀请码 */
			users.setInvitationCodeStr(invitationCode);
			
			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			/* 保存记录 */
			resultJSON = this.usersService.saveOneUsersService(users);
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("查询用户详细报错了:phone:{}", phone, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 返回结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 重置密码
	 * 忘记密码
	 * @return
	 */
	public void resetPass()
	{
		ConstatFinalUtil.LOGGER.info("======usersInsertSubmit======");
		long st = System.currentTimeMillis() ;
		JSONObject resultJSON = new JSONObject();
		
		/* 接收参数 */
		String phone = this.getPara("phone");
		String password = this.getPara("password");
		/* 用户输入的短信验证码 */
		String code = this.getPara("code");
		/* 短信验证码的Id */
		String messId = this.getPara("messId");
		
		try
		{
			Map<String,Object> condMap = new HashMap<String, Object>();
			/* 短信验证 */
			if(messId == null)
			{
				messId = "" ; 
			}
			if(!"".equalsIgnoreCase(messId))
			{
				condMap.put("id", messId);
				ASmsHistory smsHistory = this.systemService.findOneSmsHistoryService(condMap);
				/* 验证短信的验证码 */
				if(smsHistory != null && smsHistory.getCode().equalsIgnoreCase(code)
					&& smsHistory.getStatus() == 1)
				{
					/* 短信验证通过 */
					smsHistory.setStatus(ASmsHistoryEnum.STATUS_VERIFY.getStatus());
					smsHistory.setUpdateTime(new Date());
					this.systemService.updateOneSmsHistoryService(smsHistory);
				}else
				{
					/* 短信验证码不正确 */
					resultJSON.put("code", "7");
					resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("7"));
					renderJson(resultJSON);
					return ; 
				}
			}else
			{
				/* 短信验证码不正确 */
				resultJSON.put("code", "7");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("7"));
				renderJson(resultJSON);
				return ; 
			}
			
			/**
			 * 根据id查询数据
			 */
			condMap.clear();
			condMap.put("phone", phone);
			/* 创建对象,赋值 */
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				/* 更新密码 */
				users.setPassword(encryptUtil.encodePass(password));
				users.setUpdateTime(new Date());
				resultJSON = this.usersService.updateOneUsersService(users);
			}else
			{
				/* 查询不到数据 */
				resultJSON.put("code", "1");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("查询用户详细报错了:phone:{}", phone, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 返回结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
}
